package com.neusoft.servelt;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: duan
 * @Date: Create in 16:11 2020/10/19
 */
@WebServlet("/zhuanfa")
public class ZhuanfaServlet extends HttpServlet {
    /**
     * 请求转发
     *  浏览器只发送了一次请求
     *  浏览器地址不变
     *  request是同一个，所有可以传值
     *  请求方式保持不变，如果post请求被转发，则依赖是post请求
     * 重定向
     *  发送了多次请求
     *  浏览器地址会改变
     *  request不一样，所以无法传值
     *  是get请求
     *
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("zhuanfa:doPost");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.println("zhuanfa get success 终于成功了");
        System.out.println(req.getParameterMap().keySet());
        req.getRequestDispatcher("/receive").forward(req, resp);
//        resp.sendRedirect(getServletContext().getContextPath() + "/receive");//重定向
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //Dispatcher：转发
        System.out.println("zhuanfa:doGet");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.println("zhuanfa get success 终于成功了");
        req.getRequestDispatcher("/receive").forward(req, resp);

    }
}
